using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EC2Manage.com.amazonaws.s3;

namespace EC2Manage
{
    public partial class Form1 : Form
    {
        public AWSLibrary awsLib;
 
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        void listView2_DragDrop(object sender, DragEventArgs e)
        {

        }

        void listView2_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Dib))
            {
                e.Effect = DragDropEffects.Move;
            }
        }

        void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            ListViewItem lvi =  listView1.SelectedItems[0];
            ImageList il = lvi.ImageList;
            Image im = il.Images[0];
            DoDragDrop(im, DragDropEffects.Copy);
        }


        private void specifyCertificateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofn = new OpenFileDialog();
            ofn.Title = "Find AWS Certificate";
            ofn.InitialDirectory = @"C:\vs2005code\AmazonAWSLib\";
            if (ofn.ShowDialog() == DialogResult.Cancel) { return; }
            if (!File.Exists(ofn.FileName)) { return; }
            awsLib = new AWSLibrary(ofn.FileName);

            ImageForm imf = new ImageForm(awsLib);
            imf.ShowDialog();
        }

        private void buttonGo_Click(object sender, EventArgs e)
        {
            DescribeKeyPairsType dkt = new DescribeKeyPairsType();
            dkt.keySet = null;

            DescribeKeyPairsResponseType dkrt =  awsLib.ec2.DescribeKeyPairs(dkt);

            DescribeInstancesType dit = new DescribeInstancesType();
            dit.instancesSet = null;

            DescribeInstancesResponseType dirt =  awsLib.ec2.DescribeInstances(dit);

            RunInstancesType rit = new RunInstancesType();
            //rit.imageId = "ami-192cc870"; wsutest
            //rit.imageId = "ami-cf2cc8a6";
            rit.imageId = "ami-672dc90e";
            rit.keyName = "wsukey";
            rit.maxCount = 1;
            rit.minCount = 1;
            GroupItemType git = new GroupItemType();
            git.groupId = "default";
            rit.groupSet = new GroupItemType[] { git };
            UserDataType udt = new UserDataType();
            string b64String = EncodeTo64("jmarymee|ta3kw0nd0|trendiq.dyndns.org");
            udt.data = b64String;
            rit.userData = udt;

            //RegisterImageType regImg = new RegisterImageType();
            //regImg.imageLocation = "wsu08101201/image.manifest.xml";
            //RegisterImageResponseType regImgResp = awsLib.ec2.RegisterImage(regImg);

            //ReservationInfoType resInfoType = awsLib.ec2.RunInstances(rit);

            //Used to terminate an instance
            //TerminateInstancesType tit = new TerminateInstancesType();
            //TerminateInstancesItemType tiit = new TerminateInstancesItemType();
            //tiit.instanceId = dirt.reservationSet[0].instancesSet[0].instanceId;
            //tit.instancesSet = new TerminateInstancesItemType[] { tiit };
            //TerminateInstancesResponseType tirt =  awsLib.ec2.TerminateInstances(tit);
        }

        static public string EncodeTo64(string toEncode)
        {
            byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }

    }
}